package tianhaodata.integration.api.repository;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Objects;

/**
 * @Author: DDZ
 * @Date: 2023/11/08
 */
@Component
public class Rt<T> extends HashMap<String, Object> {
    public static final String CODE_TAG = "code";
    public static final String MSG_TAG = "msg";
    public static final String DATA_TAG = "data";

    public Rt() {
    }

    public Rt(int code, String msg) {
        super.put("code", code);
        super.put("msg", msg);
    }

    public Rt(int code, String msg, Object data) {
        super.put("code", code);
        super.put("msg", msg);
        if (data == null) {
            super.put("data", data);
        }

    }

    public Rt(int code, String msg, Integer su, Integer fa) {
        super.put("code", code);
        super.put("msg", msg);
        super.put("successTotal", su);
        super.put("failTotal", fa);
    }

    public static Rt success() {
        return success("操作成功");
    }

    public static Rt success(Object data) {
        return success("操作成功", data);
    }

    public static Rt success(String msg, Integer su, Integer fa) {
        return new Rt(200, msg, su, fa);
    }

    public static Rt success(String msg) {
        return success(msg, (Object) null);
    }

    public static Rt success(String msg, Object data) {
        return new Rt(200, msg, data);
    }

    public static Rt error() {
        return error("操作失败");
    }

    public static Rt error(String msg) {
        return error(msg, (Object) null);
    }

    public static Rt error(String msg, Object data) {
        return new Rt(500, msg, data);
    }

    public static Rt error(int code, String msg) {
        return new Rt(code, msg, (Object) null);
    }

    public boolean isSuccess() {
        return !this.isError();
    }

    public boolean isError() {
        return Objects.equals(200, this.get("code"));
    }

    @Override
    public Rt put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
